package com.ht.pms.ui.common;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * Http Post 请求工具类
 */
public class HttpPostUtil {

    /**
     * 编码
     */

    private static RequestConfig requestConfig;

    private static CloseableHttpClient httpClinet;

    private static final Logger log = LoggerFactory.getLogger(HttpPostUtil.class);

    static {
        requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);
        cm.setDefaultMaxPerRoute(100);
        httpClinet = HttpClients.custom().setConnectionManager(cm).build();
    }


    public static String post(String url, Map<String, String> parameterMap) {

        String result = null;
        HttpPost httpPost = new HttpPost(url);

        List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();

        for (Entry<String, String> entry : parameterMap.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setConfig(requestConfig);

            HttpResponse response = httpClinet.execute(httpPost);
            if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
                return "";
            }

            HttpEntity entity = response.getEntity();

            result = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {

            log.error(e.getMessage(), e.getStackTrace());

        } finally {
            httpPost.abort();
        }
        return result;
    }


    public static String get(String url) {

        String result = null;
        HttpGet httpGet = null;
        try {
            URL url1 = new URL(url);
            URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
            httpGet = new HttpGet(uri);
            httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpGet.setConfig(requestConfig);

            HttpResponse response = httpClinet.execute(httpGet);

            if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
                return "";
            }

            HttpEntity entity = response.getEntity();

            result = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            httpGet.abort();
        }

        return result;
    }


}
